import { setThumb, getCommentList } from '../../../api/comment'
import { getEnroll } from '../../../api/activity'
import { getTeacherDetail } from '../../../api/user'
import { getWebCastPage, getWebCastDetail, cancelCollect, collect, enroll } from '../../../api/webCast'
import { tipOffCommit } from '../../../api/tipOff'
import { formatTime } from '../../../utils/util'

const app = getApp()
Page({
  data: {
    loading: true,
    activityId: '',
    activityType: 'WEB_CAST',
    teacherDetail: {},
    mainContentActive: 'detail',
    detailData: {},
    curTime: '',
    enrollList: {
      records: [],
      enrollTotal: 300
    },
    correlationList: [],
    commentData: [],

    showReportType: false,
    reportTypes: [
      { name: '盗版侵权', value: '盗版侵权'  },
      { name: '广告或垃圾信息', value: '广告或垃圾信息'  },
      { name: '色情，淫秽，低俗信息', value: '色情，淫秽，低俗信息'  },
      { name: '反人类，反社会，反政府等信息', value: '反人类，反社会，反政府等信息'  }
    ],
    rateData:[]
  },

  onShow: function () {

  },

  onLoad: function (e) {
    let _this = this
    // 获取活动ID
    this.setData({
      activityId: e.activityId
    })

    this.init(_this.data.activityId)
  },

  back() {
    wx.navigateBack({ delta: 1 })
  },

  toPageMessage() {
    if (!getApp().globalData.token || !wx.getStorageSync('token')) {
      wx.showToast({
        title: '请先登录',
        icon: 'none',
        duration: 2000
      })
      return
    }
    wx.navigateTo({
      url: '/packageA/pages/message/message'
    })
  },

  changTab(e) {
    this.setData({
      mainContentActive: e.detail.name
    })
  },

  init(activityId) {
    const _this = this
    _this.getWebCastDetail(activityId)
    _this.getEnroll(activityId)
    _this.getCorrelation()
    _this.getCommentList()
  },
  // 获取课程详情数据
  async getWebCastDetail(activityId) {
    let res = await getWebCastDetail(activityId)
    res.createTime = formatTime(res.createTime, '{y}.{m}.{d} {h}:{i}:{s}')
    res.endTime = formatTime(res.endTime, '{y}.{m}.{d} {h}:{i}:{s}')
    this.setData({
      detailData: res,
      loading: false
    })
    this.getTeacherDetail(this.data.detailData.userId)
  },

  //获取报名数据
  async getEnroll(activityId) {
    let _this = this

    let params = {
      condition: activityId,
      pager: {
        page: 1,
        pageSize: 5
      }
    }

    let res = await getEnroll(params)
    _this.setData({
      enrollList: res
    })

  },
  // 点赞
  async onClickThumb() {
    let _this = this
    let params = {
      isThumb: !_this.data.detailData.liked,
      subjectId: this.data.activityId,
      subjectType: _this.data.activityType
    }
    await setThumb(params)
    if (!_this.data.detailData.liked) {
      this.setData({
        'detailData.likeNum': ++this.data.detailData.likeNum,
        'detailData.liked': !this.data.detailData.liked
      })
    } else {
      this.setData({
        'detailData.likeNum': --this.data.detailData.likeNum,
        'detailData.liked': !this.data.detailData.liked
      })
    }
  },
  // 点击收藏
  async onClickStar() {
    let _this = this
    let params = {
      webCastId: _this.data.activityId
    }
    if (_this.data.detailData.collected) {
      await cancelCollect(params)
      wx.showToast({
        title: '取消收藏'
      })
      _this.setData({
        'detailData.collected': false
      })

    } else {
      await collect(params)
      wx.showToast({
        title: '收藏成功'
      })
      _this.setData({
        'detailData.collected': true
      })
    }
  },
  //点击举报
  onClickInform(){
    this.setData({
      showReportType: true
    })
  },
  //隐藏举报列表
  cancelReportType() {
    this.setData({
      showReportType: false
    })
  },
  // 提交举报
  async confirmReportType(e){
    let params = {
      content: e.detail.name,
      targetType: 'live',
      targetId: this.data.activityId,
      targetUserId: this.data.teacherDetail.userId,
      targetName: this.data.detailData.title
    }
    await tipOffCommit(params)
  },
  //获取相关内容
  getCorrelation() {
    getWebCastPage({
      condition: {
        excludeIds: [this.data.activityId]
      },
      pager: {
        page: 1,
        pageSize: 2
      }
    }).then(res => {
      this.setData({
        correlationList: res.records
      })
    })
  },
  // 获取教师信息
  async getTeacherDetail(userId) {
    let res = await getTeacherDetail({ userId: userId })
    this.setData({
      teacherDetail: res
    })
  },
  // 获取评论信息
  async getCommentList() {
    let _this = this
    let params = {
      condition: {
        status: 'OK',
        subjectId: _this.data.activityId,
        subjectType: _this.data.activityType
      }
    }
    let res = await getCommentList(params)
    _this.setData({
      commentData: res.records
    })
  },
  //报名
  async onClickEnroll() {
    if (!getApp().globalData.token) {
      wx.showToast({
        title:'请先登录',
        icon: 'none'
      })
    } else {
      await enroll(this.data.activityId)
      this.setData({
        'detailData.enrolled': true
      })
      wx.showToast({
        title: '报名成功'
      })
    }
  },
  // 查看报名列表
  showEnrollPeople() {
    wx.navigateTo({
      url: `/packageA/pages/enrollPeopleList/enrollPeopleList?activityId=${ this.data.activityId }`
    })
  },
  onConsult(){
    wx.navigateTo({
      url: '/packageA/pages/msgRecords/msgRecords?userId=' + this.data.detailDate.user.userId
    })
  }
})
